//
//  ThirdViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ThirdViewController.h"
#import "GiftModel.h"
#import "GiftTableViewCell.h"
#import "BigCollectionViewCell.h"
#import "SmallCollectionViewCell.h"
#import "OriginalityViewController.h"
@interface ThirdViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UITableView *tableview;//内容 tableview
@property (nonatomic, retain) NSMutableArray *tabArr;//内容 数组
@property (nonatomic, retain) UICollectionView *bigcollectionView;//类似轮播图的collectionView
@property (nonatomic, retain) UICollectionView *smallcollectionView;//小collectionView
@property (nonatomic, retain) NSMutableArray *bigArr;//大的轮播图 数组
@property (nonatomic, retain) NSMutableArray *smallArr; //小collectionView数组
@property (nonatomic, assign) NSInteger index;
@end

@implementation ThirdViewController

-(void)dealloc
{
    [_tableview release];
    [_tabArr release];
    [_bigArr release];
    [_bigcollectionView release];
    [_smallArr release];
    [_smallcollectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];//背景颜色
    self.title = @"礼物";
    //创建tableview
    self.tableview = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    //代理
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    //添加
    [self.view addSubview:self.tableview];
    //重用池
    [self.tableview registerClass:[GiftTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableview release];
    //数据处理
    [self data];
    [self collectionViewdata];
    //头部
    [self creat];
    
}
#pragma mark - tableview必须实现的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tabArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GiftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    GiftModel *gift = _tabArr[indexPath.row];
    [cell.bgimg sd_setImageWithURL:[NSURL URLWithString:gift.cover_image_url]];
    cell.titlelabel.text = gift.title;
    //如果cell小于3 添加推荐图片
    if (indexPath.row < 3) {
        cell.tuijianimg.image = [UIImage imageNamed:@"tuijian"];
    }
    return cell;
}
#pragma mark - 自定义cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
#pragma mark - //tableview的数据
- (void)data
{
   NSString *str = @"http://api.liwushuo.com/v2/collections/174/posts?gender=2&generation=1&limit=20&offset=0";
  [AFNnetWork GETwithURL:str completion:^(id result) {
      NSArray *arr = result[@"data"][@"posts"];
      self.tabArr = [GiftModel modelHandlerWithArray:arr];
      [_tableview reloadData];
  }];
}
#pragma mark - 联动轮播数据
- (void)collectionViewdata
{

    
    NSString *str = @"http://api.liwushuo.com/v2/collections/136/posts?gender=2&generation=1&limit=20&offset=0";
  [AFNnetWork GETwithURL:str completion:^(id result) {
      NSArray *arr = result[@"data"][@"posts"];
      self.bigArr = [GiftModel modelHandlerWithArray:arr];
      [_bigcollectionView reloadData];
      [_smallcollectionView reloadData];
  }];
}
#pragma mark - tableview头部
- (void)creat
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 350)];
    headerView.backgroundColor = [UIColor whiteColor];
    [self.tableview addSubview:headerView];
    self.tableview.tableHeaderView = headerView;
    [headerView release];
    //大的collectionView 布局
    UICollectionViewFlowLayout *biglayout = [[UICollectionViewFlowLayout alloc]init];
    //行列
    biglayout.minimumInteritemSpacing = 0;
    biglayout.minimumLineSpacing = 0;
    //item
    biglayout.itemSize = CGSizeMake(self.view.frame.size.width, 200);
    //方向
    biglayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //创建
 
    self.bigcollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) collectionViewLayout:biglayout];
    //整页滑动
    self.bigcollectionView.pagingEnabled = YES;
    //代理
    self.bigcollectionView.delegate = self;
    self.bigcollectionView.dataSource = self;
    //添加
    [headerView addSubview:self.bigcollectionView];
    //背景颜色
    self.bigcollectionView.backgroundColor= [UIColor whiteColor];
    [_bigcollectionView release];
    //重用池
    [_bigcollectionView registerClass:[BigCollectionViewCell class] forCellWithReuseIdentifier:@"bigcell"];
    [biglayout release];
//    小得collectionView
    
    UICollectionViewFlowLayout *smallLayout = [[UICollectionViewFlowLayout alloc]init];
    //行列
    smallLayout.minimumInteritemSpacing = 10;
    smallLayout.minimumLineSpacing = 10;
    //item
    smallLayout.itemSize = CGSizeMake(180, 100);
    //方向
    smallLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  
    //创建
    self.smallcollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 210,self.view.frame.size.width, 100) collectionViewLayout:smallLayout];
    //代理
    self.smallcollectionView.delegate = self;
    self.smallcollectionView.dataSource = self;
    //实际内容
    self.smallcollectionView.contentSize = CGSizeMake(150 * 5, 100);
    //添加
    [headerView addSubview:self.smallcollectionView];
    //背景颜色
    self.smallcollectionView.backgroundColor= [UIColor whiteColor];
    [_smallcollectionView release];
    //重用池
    [_smallcollectionView registerClass:[SmallCollectionViewCell class] forCellWithReuseIdentifier:@"smallcell"];
    [smallLayout release];
    
    
    //创意礼物
    UILabel *gift = [[UILabel alloc]initWithFrame:CGRectMake(20, 320, 100, 30)];
    gift.text = @"创意礼物";
    gift.textColor = [UIColor grayColor];
    [headerView addSubview:gift];
    [gift release];
    //横线
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(100, 335, self.view.frame.size.width - 100, 1)];
    line.backgroundColor = [UIColor grayColor];
    [headerView addSubview:line];
    
    [line release];
}
#pragma mark - CollectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.index = indexPath.item;
    if (collectionView == self.smallcollectionView) {
        //两个图都跟着改变
        [self.bigcollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        [self.smallcollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        
//        for (GiftModel *model in self.bigArr)
//        {
//            model.isClear = NO;
//            
//        }
//        [self.bigArr[indexPath.row] setIsClear:YES];
//        [collectionView reloadData];
    }
    //如果点击的是大的collectionviewView 跳转页面
    if (collectionView == self.bigcollectionView) {
        OriginalityViewController *ori = [[OriginalityViewController alloc]init];
        GiftModel *gift = _bigArr[indexPath.row];
        ori.sid = gift.strid.stringValue;//传值
        [self.navigationController pushViewController:ori animated:YES];
    }
    
}
#pragma mark - scrollViewDelegate(scrollView 即将结束)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //判断滑动的是不是大的collectionView
    if (scrollView == self.bigcollectionView)
    {
        //偏移量
        NSInteger currentItem = (scrollView.contentOffset.x) / self.view.frame.size.width;
//       NSIndexPath *indexPath = [NSIndexPath indexPathForItem:currentItem inSection:0];
        
//        SmallCollectionViewCell *cell = [self.smallcollectionView dequeueReusableCellWithReuseIdentifier:@"smallcell" forIndexPath:indexPath];
//        cell.blackView.alpha = 0.0;
        
//        小的collectionView 也对应的滑到大的collectionView的item
        [self.smallcollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:currentItem inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        
        
//        for (GiftModel *model in self.bigArr)
//        {
//            model.isClear = NO;
//        }
//        [self.bigArr[currentItem] setIsClear:YES];
//        [self.smallcollectionView reloadData];

        
        
    }
    
}
#pragma mark - collectionView必须实现的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.bigcollectionView) {
        BigCollectionViewCell *bigcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bigcell" forIndexPath:indexPath];
        GiftModel *gift = _bigArr[indexPath.row];
        [bigcell.bgimg sd_setImageWithURL:[NSURL URLWithString:gift.cover_image_url]];
        bigcell.titleLabel.text = gift.title;

        return bigcell;
    }
  if (collectionView == self.smallcollectionView)
   {
       SmallCollectionViewCell *smallcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"smallcell" forIndexPath:indexPath];
       GiftModel *gift = _bigArr[indexPath.row];
       [smallcell.bgimg sd_setImageWithURL:[NSURL URLWithString:gift.cover_image_url]];
       
       return smallcell;
   }
  else
  {
      return nil;
  }
}
#pragma mark - tableview 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OriginalityViewController *originality = [[OriginalityViewController alloc]init];
    GiftModel *g = _tabArr[indexPath.row];
    originality.sid = g.strid.stringValue;
    [self.navigationController pushViewController:originality animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
